package com.wxs.shiro.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.wxs.common.pojo.Role;
import com.wxs.common.pojo.RoleGroup;
import com.wxs.common.service.RoleGroupService;
import com.wxs.common.service.RoleService;
import com.wxs.common.utils.DataGridModel;
import com.wxs.common.utils.DataResult;
import com.wxs.common.utils.JsonResult;

/**
 * 角色组管理
 * 
 * @author admin
 *
 */
@Controller
@RequestMapping("/rolegroup/")
public class RoleGroupController {
	@Autowired
	private RoleGroupService roleGroupService;
	@Autowired
	private RoleService roleService;

	@RequestMapping("main.jhtml")
	public ModelAndView main() {
		ModelAndView mv = new ModelAndView("rolegroup/list");
		return mv;
	}

	@ResponseBody
	@RequestMapping("list.jhtml")
	public DataResult list(String name, Boolean enabled, DataGridModel dgm) {
		return roleGroupService.queryPage(name, enabled, dgm);
	}

	@RequestMapping("edit.jhtml")
	public ModelAndView edit(@RequestParam(value = "id", required = true) Integer id) {
		ModelAndView mv = new ModelAndView("rolegroup/edit");
		mv.addObject("roleGroup", roleGroupService.findById(id));
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = "saveEdit.jhtml", produces = "text/html;charset=UTF-8")
	public String saveEdit(RoleGroup rolegroup) {
		try {
			if (roleGroupService.saveEdit(rolegroup) > 0) {
				return JsonResult.toSuccess(null, "修改成功！");
			} else {
				return JsonResult.toError(null, "修改失败！");
			}
		} catch (Exception e) {
			return JsonResult.toError(e.getMessage(), "修改失败！");
		}
	}

	@ResponseBody
	@RequestMapping("doCheckName")
	public Boolean checkName(@RequestParam(value = "name", required = true) String name, Integer id) {
		return roleGroupService.checkName(name, id);
	}

	@ResponseBody
	@RequestMapping(value = "doDeleteRole", produces = "text/html;charset=UTF-8")
	public String doDeleteRole(@RequestParam(value = "id", required = true) Integer id) {
		try {
			roleGroupService.deleteById(id);
			return JsonResult.toSuccess(null, "删除成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.toError(e.getMessage(), "删除失败！");
		}
	}

	@RequestMapping("add.jhtml")
	public String add() {
		return "rolegroup/add";
	}

	@ResponseBody
	@RequestMapping(value = "saveAdd.jhtml", produces = "text/html;charset=UTF-8")
	public String saveAdd(RoleGroup rolegroup) {
		try {
			rolegroup.setId(null);
			if (roleGroupService.saveAdd(rolegroup) > 0) {
				return JsonResult.toSuccess(null, "保存成功！");
			} else {
				return JsonResult.toError(null, "保存失败！");
			}
		} catch (Exception e) {
			return JsonResult.toError(e.getMessage(), "保存失败！");
		}
	}

	@RequestMapping("set.jhtml")
	public ModelAndView set(@RequestParam(value = "id", required = true) Integer id) {
		ModelAndView mv = new ModelAndView("rolegroup/set");
		List<Role> roles = roleService.queryForList(id);
		String json = JSON.toJSONString(roles);
		mv.addObject("roles", json);
		mv.addObject("rolegroupid", id);
		return mv;
	}

	@ResponseBody
	@RequestMapping(value = "saveRoles.jhtml", produces = "text/html;charset=UTF-8")
	public String saveRoles(String[] roleids, Integer rolegroupid) {
		try {
			roleGroupService.saveRoles(roleids, rolegroupid);
			return JsonResult.toSuccess(null, "保存成功！");
		} catch (Exception e) {
			return JsonResult.toError(null, "保存失败！");
		}
	}
}
